﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Net;
using Arcadium.Graphics;
using Arcadium.Screens.Menus.MenuHelpers;
using Arcadium.Screens.Menus.Sidebar;

namespace Arcadium
{
    /// <summary>
    /// -.-
    /// </summary>
    class ActivationScreen : MenuScreen
    {
        #region Fields

        MenuEntry pressStartMenuEntry;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public ActivationScreen()
            : base(null)
        {
            // Create the menu entry.
            pressStartMenuEntry = new MenuEntry(Resources.PressStart);

            // Hook up menu event handler.
            pressStartMenuEntry.Selected += PressStartMenuEntrySelected;

            // Add the entry to the menu.
            MenuEntries.Add(pressStartMenuEntry);

            // TODO: Activation screen has only one entry, which connot be selected(?) Seems okay...
            SelectedEntry = -1;
        }

        #endregion

        #region Handle Input

        public override void HandleInput(InputState input)
        {
            PlayerIndex playerIndex;

            // Override the selection, cause there's just one selection that can be made.
            if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
            {
                OnSelectEntry(0, playerIndex);
            }
            else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
            {
                OnCancel(playerIndex);
            }
        }

        /// <summary>
        /// Event handler for when the Press Start menu entry is selected.
        /// </summary>
        void PressStartMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            //ControllingPlayer = e.PlayerIndex;

            // Make the gamer sign in, at least with a Local player, but a a more privileged profile would be better.
            ProfileSignInScreen profileSignIn = new ProfileSignInScreen(NetworkSessionType.Local);
            profileSignIn.ControllingPlayer = e.PlayerIndex;

            // Hook up an event so once the ProfileSignInScreen is happy,
            // it will activate the MainMenu
            profileSignIn.ProfileSignedIn += delegate
            {
                List<NetworkSessionType> availableSessionTypes = new List<NetworkSessionType>() { 
                    NetworkSessionType.PlayerMatch,
                    NetworkSessionType.SystemLink,
                    NetworkSessionType.Local};

                SignedInGamer signedInGamer = SignedInGamer.SignedInGamers[e.PlayerIndex];

                // Success, so now we have a profile, but can it play online?
                if (!signedInGamer.IsSignedInToLive || !signedInGamer.Privileges.AllowOnlineSessions)
                {
                    availableSessionTypes.Remove(NetworkSessionType.PlayerMatch);
                }

                MainMenuScreen mainMenuScreen = new MainMenuScreen();
                try
                {
                    // Create a sidbar to take care of the session and advance to main menu
                    ScreenManager.AddScreen(mainMenuScreen, e.PlayerIndex);
                    ScreenManager.AddScreen(new MenuSidebar(availableSessionTypes, e.PlayerIndex), e.PlayerIndex);
                }
                catch (Exception exception)
                {
                    ScreenManager.RemoveScreen(mainMenuScreen);
                    NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
                    ScreenManager.AddScreen(errorScreen, e.PlayerIndex);
                }
            };

            // Activate the ProfileSignInScreen.
            ScreenManager.AddScreen(profileSignIn, e.PlayerIndex);
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            string message = Resources.ConfirmExitSample;

            MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.Game.Exit();
        }

        #endregion
    }
}
